/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rpg.gameSkeleton;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import rpg.Dungeon;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import rpg.PlayerCharacter;
import rpg.gameSkeleton.Constant;
import rpg.controller.HeroCollisionManager;
import views.PlayerMapBar;

/**
 *
 * @author Emmanouil Samatas
 */
public class Board extends JPanel implements ActionListener {

    private PlayerCharacter myHero;
    private HeroCollisionManager myHeroController;
    private Timer timer;
    private Dungeon dungeon;
    private List<String> map;
    private PlayerMapBar mapBar = new PlayerMapBar();

    public Board(Dungeon dungeon, PlayerCharacter pc, HeroCollisionManager myHeroController) {
        this.dungeon = dungeon;
        map = dungeon.getMap();
        addKeyListener(new TAdapter());
        setFocusable(true);
        setBackground(Color.BLACK);
        setLayout(new BorderLayout());
        setDoubleBuffered(true);
        this.myHero = pc;
        this.myHeroController = myHeroController;
        drawBar();
        this.add(mapBar, BorderLayout.SOUTH);

        timer = new Timer(200, this);
        timer.start();

    }

    private void paintMap(Graphics2D g2d) {

        for (int y = 0; y < map.size(); y++) {
            for (int x = 0; x < map.get(y).length(); x++) {
                if (dungeon.getMapPoint(x, y).equals("w")) {
                    g2d.drawImage(dungeon.getWall(), x * (Constant.WALL_IMAGE_SIZE), y * (Constant.WALL_IMAGE_SIZE), this);
                }
                if (dungeon.getMapPoint(x, y).equals("e")) {
                    g2d.drawImage(dungeon.getMilitary(), x * (Constant.ENEMY_IMAGE_SIZE), y * (Constant.ENEMY_IMAGE_SIZE), this);
                }
            }
        }



    }

    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;

        paintMap(g2d);
        paintHero(g2d);
        drawBar();
        //Toolkit.getDefaultToolkit().sync();
        //g.dispose();
    }

    private void paintHero(Graphics2D g2d) {
        Image heroSmallImage = myHero.getSmallImage();
        int drawX = myHero.getX() * Constant.PLAYER_IMAGE_SIZE;
        int drawY = myHero.getY() * Constant.PLAYER_IMAGE_SIZE;
        g2d.drawImage(heroSmallImage, drawX, drawY, this);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {

        myHero.move();
        repaint();
    }

    private class TAdapter extends KeyAdapter {

        public void keyReleased(KeyEvent e) {
            myHeroController.keyReleased(e);
        }

        public void keyPressed(KeyEvent e) {
            myHeroController.keyPressed(e);
        }
    }
    
    private void drawBar(){
        mapBar.playerName.setText(myHero.getCharacterName()+" lvl."+String.valueOf(myHero.getLevel()));
        mapBar.playerDamage.setText("Damage: " + String.valueOf(myHero.getDamage()));
        mapBar.playerDefence.setText("Defence: " + String.valueOf(myHero.getDefence()));
        
        mapBar.playerHP.setMaximum(myHero.getMaxHealthPoints());
        mapBar.playerHP.setValue(myHero.getHealthPoints());
        mapBar.playerHP.setString("HP: " +String.valueOf(myHero.getHealthPoints()) +"/" + String.valueOf(myHero.getMaxHealthPoints()) );
        
        mapBar.playerMP.setMaximum(myHero.getMaxManaPoints());
        mapBar.playerMP.setValue(myHero.getManaPoint());
        mapBar.playerMP.setString("MP: "+ String.valueOf(myHero.getManaPoint()) +"/"+ String.valueOf(myHero.getMaxManaPoints()));
        
        mapBar.playerXP.setMaximum(myHero.getXpForNextLevel());
        mapBar.playerXP.setValue(myHero.getExperience());
        
    }
}
